import { action, observable } from "mobx";
import * as API from "../../apis/modetab";
import {message} from 'antd';
import { WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
// import _ from 'lodash';
// const getLabel = WeaLocaleProvider.getLabel;

export default class TabStore {
    @observable tabs: any[];
    @observable existTabs: any[];
    tablename:String;

    @action
    initData = (modeid, billid) => {
        //初始化数据
        let parm = { modeid: modeid, billid: billid };
        API.getTabs(parm).then(({ tabs,tablename, existTabs,status, error }: any) => {
            this.tabs =tabs;
            this.tablename=tablename;
            this.existTabs=existTabs;
        });
    }

    @action
    updateDataTab = (parm) => {
        //初始化数据
        API.updateDataTab(parm).then(({status}: any) => {
            this.initData(parm.modeid,parm.billid);
        });
    }

    @action
    saveSingtonTab = (parm,callback?) => {
        //初始化数据
        API.saveSingtonTab(parm).then(({status,id}: any) => {
            if(status==2){//标签名已存在
                message.error(getLabel(25434, " 标签名已存在！"));
            }else if (status==1){
                if(id!=undefined){//新增保存的同时要给数据贴上标签
                    this.updateDataTabForadd(id,parm);
                    if(callback){
                        callback({...parm,id:id});
                    }
                }else{
                    this.initData(parm.modeid,parm.billid);
                }
            }
        });
    }

    updateDataTabForadd(id,parm){
        let modelableid = '';
        this.existTabs.map((tab) => {
            modelableid += tab.id + ',';
        });
        modelableid+=id;
        let parm2 = { operate: '0', modelableid: modelableid,  tablename: this.tablename, tabid: id,...parm };
        this.updateDataTab(parm2);
    }



    @action
    delSingtonTab = (parm) => {
        //初始化数据
        API.delSingtonTab(parm).then(({status}: any) => {
            if (status==1){
                message.info(getLabel(83472,"删除成功！"));
                this.initData(parm.modeid,parm.billid);
            }
        });
    }




}
